using UnityEngine;

public class ChallengeUI : MonoBehaviour
{
	private GameObject uiobj;

	public UIWidget lastwi;

	public UIWidget nextwi;

	public UIEventListener lastbtn;

	public UIEventListener nextbtn;

	public Transform scrollpoint;

	public Challenge_item[] challengeitem;

	public float rotationY;

	public float preRotationY;

	private float sensitivityY = 0.5f;

	private float deltaposx;

	private float starttime;

	private float intaltime;

	private float endval;

	private float scrollrate;

	private float scrollstep = 50f;

	private bool scrollmoveflag;

	private void Start()
	{
		lastbtn.onClick = OnClicklastBtn;
		nextbtn.onClick = OnClicknextBtn;
	}

	public void init_ui_info(int id)
	{
		setbeginscroll(id);
		MenuPlayerprefs.instance.readChallenge();
		MenuPlayerprefs.instance.readAllLevel();
		for (int i = 0; i < 4; i++)
		{
			challengeitem[i].enable();
		}
		Platform.flurryEvent_challenge(0, 3, 0);
	}

	public int Gethintinfo()
	{
		MenuPlayerprefs.instance.readchall_hint();
		MenuPlayerprefs.instance.readChallenge();
		MenuPlayerprefs.instance.readAllLevel();
		for (int i = 0; i < 4; i++)
		{
			if (challengeitem[i].enable())
			{
				return i;
			}
		}
		return -1;
	}

	public void setbeginscroll(int num)
	{
		rotationY = (float)num * 50f;
		scrollpoint.localRotation = Quaternion.AngleAxis(rotationY, Vector3.up);
	}

	private void Update()
	{
		if (UnityEngine.Input.touchCount > 0 && UnityEngine.Input.GetTouch(0).phase == TouchPhase.Moved && MenuBackController.instance.curstate == MenuUIState.challengeui)
		{
			scrollmoveflag = false;
			preRotationY = rotationY;
			Vector2 deltaPosition = UnityEngine.Input.GetTouch(0).deltaPosition;
			if (deltaPosition.x != 0f)
			{
				Vector2 deltaPosition2 = UnityEngine.Input.GetTouch(0).deltaPosition;
				deltaposx = deltaPosition2.x;
			}
			float num = rotationY;
			Vector2 deltaPosition3 = UnityEngine.Input.GetTouch(0).deltaPosition;
			rotationY = num - deltaPosition3.x * sensitivityY;
			if (rotationY <= 0f)
			{
				rotationY = 0f;
			}
			if (rotationY >= 150f)
			{
				rotationY = 150f;
			}
			scrollpoint.localRotation = Quaternion.AngleAxis(rotationY, Vector3.up);
		}
		if (UnityEngine.Input.touchCount == 0)
		{
			if (deltaposx < 0f)
			{
				if ((int)rotationY % 50 > 8)
				{
					OnClicknextBtn(null);
				}
				else if ((int)rotationY % 50 > 0)
				{
					OnClicklastBtn(null);
				}
			}
			if (deltaposx > 0f)
			{
				if ((int)rotationY % 50 > 42)
				{
					OnClicknextBtn(null);
				}
				else if ((int)rotationY % 50 > 0)
				{
					OnClicklastBtn(null);
				}
			}
			deltaposx = 0f;
		}
		if (scrollmoveflag)
		{
			if (Time.time - starttime < intaltime)
			{
				rotationY += Time.deltaTime * scrollrate;
			}
			else
			{
				rotationY = endval;
				scrollmoveflag = false;
			}
			scrollpoint.localRotation = Quaternion.AngleAxis(rotationY, Vector3.up);
		}
		if (rotationY < 20f)
		{
			lastwi.alpha = 0f;
			lastwi.gameObject.SetActive(false);
		}
		else if (lastwi.alpha != 1f)
		{
			lastwi.alpha = 1f;
			lastwi.gameObject.SetActive(true);
		}
		if (rotationY > 130f)
		{
			nextwi.alpha = 0f;
			nextwi.gameObject.SetActive(false);
		}
		else if (nextwi.alpha != 1f)
		{
			nextwi.alpha = 1f;
			nextwi.gameObject.SetActive(true);
		}
	}

	public void OnClicklastBtn(GameObject btn)
	{
		if (!scrollmoveflag && !(rotationY <= 0f))
		{
			if (btn != null)
			{
				MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
			}
			scrollmoveflag = true;
			starttime = Time.time;
			intaltime = 0.8f;
			scrollrate = (0f - scrollstep) / intaltime;
			endval = getEndVal(isup: false);
			intaltime = (rotationY - endval) / scrollstep * intaltime;
		}
	}

	public void OnClicknextBtn(GameObject btn)
	{
		if (!scrollmoveflag && !(rotationY >= 150f))
		{
			if (btn != null)
			{
				MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
			}
			scrollmoveflag = true;
			starttime = Time.time;
			intaltime = 0.8f;
			scrollrate = scrollstep / intaltime;
			endval = getEndVal(isup: true);
			intaltime = (endval - rotationY) / scrollstep * intaltime;
		}
	}

	public float getEndVal(bool isup)
	{
		if (isup)
		{
			return (float)((int)rotationY / 50 + 1) * 50f;
		}
		if ((int)rotationY % 50 > 0)
		{
			return (float)((int)rotationY / 50) * 50f;
		}
		return (float)((int)rotationY / 50 - 1) * 50f;
	}

	public void enable(int startid)
	{
		if (uiobj == null)
		{
			uiobj = base.gameObject;
		}
		NGUITools.SetActive(uiobj, state: true);
		init_ui_info(startid);
	}

	public void disable()
	{
		NGUITools.SetActive(uiobj, state: false);
	}
}
